//
//  KKBLEModel.m
//
//  Created by zorro on 2017/11/13.
//  Copyright © 2017年 zorro. All rights reserved.
//

#import "KKBLEModel.h"

@implementation KKBLEModel

- (instancetype)init {
    self = [super init];
    if (self) {
        _bleName          = @"";
        _bleUUID          = @"";
        _bleVersion       = 0;
        _bleOnlineVersion = 0;
        _bleRSSI          = @"";
        _bleMAC           = @"";
        _isBinding        = NO;
        _batteryQuantity  = 0;
        _bleFunction      = 0;
        _bleCustomerID    = 0;
        _bleAdvID         = 0;
        _function         = 0;
        _fullPower = 0;
        _currentPower = 0;
    }
    return self;
}

+ (void)initialize {
    [self removePropertyWithColumnName:@"peripheral"];
    /*
     [self removePropertyWithColumnName:@"bleRSSI"];
     [self removePropertyWithColumnName:@"bleVersion"];
     [self removePropertyWithColumnName:@"bleOnlineVersion"];
     [self removePropertyWithColumnName:@"bleElec"];
     [self removePropertyWithColumnName:@"isConnected"];
     [self removePropertyWithColumnName:@"isRepeatConnect"];
     [self removePropertyWithColumnName:@"isInitiative"]; */
}

+ (instancetype)initWithUUID:(NSString *)uuid {
    KKBLEModel *model = [[KKBLEModel alloc] init];
    
    model.bleUUID = uuid;

    return model;
}

// 根据uuid创建模型 并保存到数据库
+ (KKBLEModel *)getModelFromDBWtihUUID:(NSString *)uuid {
     NSString *where = [NSString stringWithFormat:@"bleUUID = '%@'", uuid];
     KKBLEModel *model = [KKBLEModel searchSingleWithWhere:where orderBy:nil];

     if (!model)
     {
     model = [KKBLEModel initWithUUID:uuid];
     }
    
    return model;
}

// 主建
+ (NSString *)getPrimaryKey {
    return @"bleUUID";
}

// 表名
+ (NSString *)getTableName {
    return @"KKBLEModel";
}

// 表版本
+ (int)getTableVersion {
    return 1;
}

// 更新当前模型到数据库.
- (void)updateToDBSafely {
}

// 判断当前蓝牙设备是否连接
- (BOOL)isConnected {
    if (_peripheral.state == CBPeripheralStateConnected) {
        return YES;
    }else{
           return NO;
    }
}

@end

